package com.extsci.webmail.web.controller;


import cc.ccoke.validate.annotation.ParamVerify;
import cc.ccoke.validate.type.VerifyType;
import com.extsci.webmail.common.entity.communicate.CommunicateGroup;
import com.extsci.webmail.common.entity.communicate.CommunicateUser;
import com.extsci.webmail.common.entity.http.ObjectRestResponse;
import com.extsci.webmail.common.entity.http.ResponseStatus;
import com.extsci.webmail.web.service.CommunicateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 通讯录
 * @author ccoke
 */
@RestController
@RequestMapping("/communicate/group")
public class CommunicateController {
    private final CommunicateService communicateService;

    @Autowired
    public CommunicateController(CommunicateService communicateService) {
        this.communicateService = communicateService;
    }

    /**
     * 获取整个通讯录
     */
    @GetMapping("/user/all")
    public ObjectRestResponse<List<CommunicateGroup>> getAll(){
        List<CommunicateGroup> groups = communicateService.getAll();
        return new ObjectRestResponse<>(ResponseStatus.SUCCESS, groups);
    }

    /**
     * 获取通讯录分组列表
     */
    @GetMapping
    public ObjectRestResponse<List<CommunicateGroup>> getGroups(){
        List<CommunicateGroup> groups = communicateService.getGroups();
        return new ObjectRestResponse<>(ResponseStatus.SUCCESS, groups);
    }

    /**
     * 通过分组Uuid获取联系人列表
     */
    @GetMapping("/{group_uuid}/users")
    public ObjectRestResponse<List<CommunicateUser>> getUsers(@PathVariable("group_uuid") String groupUuid) {
        List<CommunicateUser> users = communicateService.getUsers(groupUuid);
        return new ObjectRestResponse<>(ResponseStatus.SUCCESS, users);
    }

    /**
     * 添加分组
     */
    @PostMapping
    @ParamVerify
    public ObjectRestResponse<List<CommunicateGroup>> postGroup(@RequestParam("name") String groupName){
        return communicateService.addGroup(groupName);
    }

    /**
     * 根据分组Uuid添加联系人
     */
    @ParamVerify
    @PostMapping("/{group_uuid}/user")
    public ObjectRestResponse<List<CommunicateUser>> postUser(@PathVariable("group_uuid") String groupUuid, @RequestBody List<CommunicateUser> users){
        return communicateService.addUsers(groupUuid, users);
    }

    /**
     * 修改分组名
     */
    @ParamVerify
    @PutMapping("/{group_uuid}")
    public ObjectRestResponse<List<CommunicateGroup>> putGroup(@PathVariable("group_uuid") String groupUuid, @RequestParam("newname") String name) {
        return communicateService.modifyGroup(groupUuid, name);
    }

    /**
     * @describe 修改联系人信息
     */
    @ParamVerify(type = VerifyType.IGNORE, value = {"user.groupUuid", "user.createAt"})
    @PutMapping("/{group_uuid}/user")
    public ObjectRestResponse<List<CommunicateUser>> putUser(@PathVariable("group_uuid") String groupUuid, @RequestBody CommunicateUser user) {
        return communicateService.modifyUser(groupUuid, user);
    }

    /**
     * 删除分组
     */
    @DeleteMapping("/{group_uuid}")
    public ObjectRestResponse<List<CommunicateGroup>> deleteGroup(@PathVariable("group_uuid") String groupUuid) {
        return communicateService.removeGroup(groupUuid);
    }

    /**
     * 删除联系人(多选)
     */
    @DeleteMapping("/{group_uuid}/users")
    public ObjectRestResponse<List<CommunicateUser>> deleteUsers(@PathVariable("group_uuid") String groupUuid, @RequestBody List<String> users) {
        communicateService.removeUsers(groupUuid, users);
        return new ObjectRestResponse<>(ResponseStatus.SUCCESS, communicateService.getUsers(groupUuid));
    }

    /**
     * 将联系人移动分组(多选)
     */
    @PutMapping("/{group_uuid}/newgroup/{new_group_uuid}")
    public ObjectRestResponse<List<CommunicateGroup>> moveUsers(@PathVariable("group_uuid") String groupUuid, @PathVariable("new_group_uuid") String newGroupUuid, @RequestBody List<String> users) {
        return communicateService.moveUsers(groupUuid, newGroupUuid, users);
    }
}
